<template>
  <!-- 搜索工作栏 -->
  <div class="p-[var(--app-content-padding)]">
    <ContentWrap class="form-card">
      <el-form class="-mb-15px mt-4px" :model="queryParams" ref="queryFormRef" :inline="true">
        <el-form-item label="模版编码" prop="mail">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入模版编码"
            clearable
            @keyup.enter="setSearchParams"
            class="!w-240px"
          />
        </el-form-item>

        <el-form-item label="模版名称" prop="username">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入模版名称"
            clearable
            @keyup.enter="setSearchParams"
            class="!w-240px"
          />
        </el-form-item>

        <el-form-item label="邮箱账号" prop="accountId">
          <el-select
            v-model="queryParams.accountId"
            placeholder="请选择邮箱账号"
            class="!w-240px"
            clearable
            @keyup.enter="setSearchParams"
          >
            <el-option
              v-for="item in mailList"
              :key="item.id"
              :label="item.mail"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="开启状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择开启状态"
            class="!w-240px"
            clearable
            @keyup.enter="setSearchParams"
          >
            <el-option
              v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="创建时间" prop="createTime">
          <el-date-picker
            v-model="queryParams.createTime"
            type="datetimerange"
            range-separator="-"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            value-format="YYYY-MM-DD HH:mm:ss"
            class="!w-240px"
          />
        </el-form-item>

        <el-form-item class="float-right" style="margin-right: 0">
          <el-button type="primary" @click="setSearchParams">
            <Icon icon="ep:search" />
            <span class="mt-1px">搜索</span>
          </el-button>
          <el-button @click="resetQuery">
            <Icon icon="ep:refresh" />
            <span class="mt-1px">重置</span>
          </el-button>
        </el-form-item>
      </el-form>
    </ContentWrap>

    <!-- 列表 -->
    <ContentWrap class="mb-0">
      <div class="flex justify-between mb-[12px]">
        <div>
          <el-button
            type="primary"
            @click="openForm('create')"
            v-hasPermi="['system:mail-template:create']"
          >
            <Icon icon="ep:plus" class="mr-5px" /> 新增
          </el-button>
        </div>
      </div>

      <Table
        :columns="allSchemas.tableColumns"
        :data="tableObject.tableList"
        :loading="tableObject.loading"
        v-model:pageSize="tableObject.pageSize"
        v-model:currentPage="tableObject.currentPage"
        :height="tableHeight"
      >
        <template #action="{ row }">
          <el-tooltip content="测试" placement="top">
            <el-button
              link
              type="primary"
              @click="openSendForm(row.id)"
              v-hasPermi="['system:mail-template:send-mail']"
            >
              <Icon icon="iconoir:test-tube" />
            </el-button>
          </el-tooltip>

          <el-tooltip content="编辑" placement="top">
            <el-button
              link
              type="primary"
              @click="openForm('update', row.id)"
              v-hasPermi="['system:mail-template:update']"
            >
              <Icon icon="iconamoon:edit-light" />
            </el-button>
          </el-tooltip>

          <el-tooltip content="删除" placement="top">
            <el-button
              link
              type="danger"
              v-hasPermi="['system:mail-template:delete']"
              @click="handleDelete(row.id)"
            >
              <Icon icon="material-symbols:delete-outline" />
            </el-button>
          </el-tooltip>
        </template>
      </Table>
      <!-- 分页 -->
      <Pagination
        v-model:limit="tableObject.pageSize"
        v-model:page="tableObject.currentPage"
        :total="tableObject.total"
      />
    </ContentWrap>
  </div>

  <!-- 表单弹窗：添加/修改 -->
  <MailTemplateForm ref="formRef" @success="getList" />

  <!-- 表单弹窗：发送测试 -->
  <MailTemplateSendForm ref="sendFormRef" />
</template>
<script lang="ts" setup>
import { allSchemas } from './template.data'
import * as MailTemplateApi from '@/api/system/mail/template'
import MailTemplateForm from './MailTemplateForm.vue'
import MailTemplateSendForm from './MailTemplateSendForm.vue'
import * as MailAccountApi from '@/api/system/mail/account'
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { useTableHeight } from '@/hooks/web/useTableHeight'

defineOptions({ name: 'SystemMailTemplate' })
const queryFormRef = ref() // 搜索的表单
const queryParams = reactive({
  pageNo: 1,
  pageSize: 20,
  code: '',
  name: '',
  accountId: undefined,
  status: undefined,
  createTime: []
})

const mailList = ref<any>([])

const { tableHeight } = useTableHeight({
  extraHeight: 208
})

// tableObject：表格的属性对象，可获得分页大小、条数等属性
// tableMethods：表格的操作对象，可进行获得分页、删除记录等操作
const { tableObject, tableMethods } = useTable({
  getListApi: () => MailTemplateApi.getMailTemplatePage(queryParams), // 分页接口
  delListApi: MailTemplateApi.deleteMailTemplate // 删除接口
})
// 获得表格的各种操作
const { getList, setSearchParams } = tableMethods

const resetQuery = () => {
  queryFormRef.value.resetFields()
  getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = (id: number) => {
  tableMethods.delList(id, false)
}

/** 发送测试操作 */
const sendFormRef = ref()
const openSendForm = (id: number) => {
  sendFormRef.value.open(id)
}

const getMailList = async () => {
  const res = await MailAccountApi.getSimpleMailAccountList()
  mailList.value = res
}

/** 初始化 **/
onMounted(() => {
  getList()
  getMailList()
})
</script>
